#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright [Gtlions Lai].
# Create Date:
# Update Date:
"""summarization ahout this script.

detail ahout this script

    Class(): summarization about Class
    ...
    function(): summarization about function
    ...
"""
__authors__ = '"Gtlions Lai" <gtlions.l@qq.com>'

import os
import sys
import time

if len(sys.argv) != 3:
    print "Usage ./checkneterrors.py times[int] sleeps[int]"
    sys.exit(1)
else:
    times = int(sys.argv[1])
    sleeps = int(sys.argv[2])
    if (not times) or (not sleeps):
        print 'times and sleeps must int(>0).'
        sys.exit(1)

for i in range(times):
    print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    ret = os.popen('netstat -su|grep errors').readlines()
    pre = int(ret[0].replace('\n', '').split()[0])
    rbe = int(ret[1].replace('\n', '').split()[0])
    if i == 0:
        print ('packet receive errors value:    ' + str(pre)).ljust(50)
        print ('receive buffer errors value:    ' + str(rbe)).ljust(50)
    else:
        print ('packet receive errors value:    ' + str(pre)
               ).ljust(50), ('gap values:    ' + str(pre - p_pre)).ljust(30)
        print ('receive buffer errors value:    ' + str(rbe)
               ).ljust(50), ('gap values:    ' + str(rbe - p_rbe)).ljust(30)
    print ' '
    p_pre = pre
    p_rbe = rbe
    time.sleep(sleeps)
